<template>
  <div class="icons">
    <swiper :options="swiperOption" v-if="Icon">
      <swiper-slide v-for="(page,index) in pages" :key="index">
        <div class="icon" v-for="item in page" :key="item.id">
          <div class="icon-img">
            <img :src="item.imgUrl" alt class="icon-content">
          </div>
          <p class="icon-desc">{{item.desc}}</p>
        </div>
      </swiper-slide>
    </swiper>
  </div>
</template>
<script>
export default {
  created() {
    console.log(this.list);
  },
  props: ["list"],
  data() {
    return {
      swiperOption: {
        pagination: ".swiper-pagination",
        loop: true
      }
    };
  },
  //计算属性 将一维数组拆分为二维数组
  computed: {
    pages() {
      const pages = [];
      this.list.forEach((item, i) => {
        const page = Math.floor(i / (this.list.length - 1));
        if (!pages[page]) {
          pages[page] = [];
        }
        pages[page].push(item);
      });
      return pages;
    },
    Icon(){
      return this.list.length
    }
  }
};
</script>
<style lang="stylus" scoped>
@import '~styles/varibles.styl';
@import '~styles/mixins.styl';

.icons >>>.swiper-slide {
  height: 0;
  overflow: hidden;
  padding-bottom: 50%;
}

.icons {
  .icon {
    position: relative;
    float: left;
    overflow: hidden;
    height: 0;
    width: 25%;
    padding-bottom: 25%;

    .icon-img {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0.44rem;
      box-sizing: border-box;
      padding: 0.1rem;

      .icon-content {
        height: 100%;
        display: block;
        margin: 0 auto;
      }
    }

    .icon-desc {
      position: absolute;
      width: 1.86rem;
      bottom: 0;
      left: 0;
      right: 0;
      text-align: center;
      line-height: 0.44rem;
      height: 0.44rem;
      color: $text;
      ellipsis();
    }
  }
}
</style>